using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;

namespace FpFrame
{
    public class MoveUIAnimation : IUIAnimation
    {
        public float duration = 0.3f;
        public Vector2 offset = new Vector2(0, -200);

        public UniTask PlayShow(UIView view)
        {
            var tcs = new UniTaskCompletionSource();
            var rt = view.transform as RectTransform;
            if (rt == null)
            {
                tcs.TrySetResult();
                return tcs.Task;
            }

            DOTween.Kill(rt);
            Vector2 end = rt.anchoredPosition;
            rt.anchoredPosition = end + offset;

            rt.DOAnchorPos(end, duration)
                .SetEase(Ease.OutQuad)
                .SetUpdate(true)
                .OnComplete(() => tcs.TrySetResult());

            return tcs.Task;
        }

        public UniTask PlayHide(UIView view)
        {
            var tcs = new UniTaskCompletionSource();
            var rt = view.transform as RectTransform;
            if (rt == null)
            {
                tcs.TrySetResult();
                return tcs.Task;
            }

            DOTween.Kill(rt);
            Vector2 end = rt.anchoredPosition + offset;

            rt.DOAnchorPos(end, duration)
                .SetEase(Ease.InQuad)
                .SetUpdate(true)
                .OnComplete(() => tcs.TrySetResult());

            return tcs.Task;
        }
    }
}